package jp.co.lab3;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class DiaryMemo_SQL extends Activity {
	
	private long mDate = 0;
	private static final String TAG = "Daily Memo";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.edit);
        
        Intent intent = getIntent();
        if (intent != null) {
        	if (intent.hasExtra(Defines.KEY_DATE)) {
        		mDate = intent.getLongExtra(Defines.KEY_DATE, 0);
        	}
        }
        
        String content = Utils.getContents(this, mDate, "");
        String date = Defines.sFmt.format(mDate);
        
		TextView txtsubject = (TextView) findViewById(R.id.TextView01);
		EditText txtcontent = (EditText) findViewById(R.id.EditText01);

		txtsubject.setText(date);
		txtcontent.setText(content);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_diary_memo_sql, menu);
        return true;
    }

	public void onClickButton(View view) {
		switch (view.getId()) {
		case R.id.Button01: {
			AlertDialog.Builder dlg = new AlertDialog.Builder(this);
			dlg.setTitle(getString(R.string.lbSaveTitle));
			dlg.setPositiveButton(getString(R.string.lbYes),
			        new DialogInterface.OnClickListener() {
				        @Override
				        public void onClick(DialogInterface dialog, int which) {
					        // 保存処理
                            EditText txtcontent = 
                                (EditText) findViewById(R.id.EditText01);
					        String content = txtcontent.getText().toString();

					        // 保存されたデータを確認する
					        Log.d(TAG, "content=" + content);

					        String oldcontent = Utils.getContents(DiaryMemo_SQL.this, mDate, null);
					        
					        if (oldcontent != null) {
					        	Utils.updateContents(DiaryMemo_SQL.this, mDate, content);
					        } else {
					        	Utils.createContents(DiaryMemo_SQL.this, mDate, content);
					        }
					        
					        finish();
				        }
			        });
			dlg.setNegativeButton(getString(R.string.lbNo),
			        new DialogInterface.OnClickListener() {
				        @Override
				        public void onClick(DialogInterface dialog, int which) {
					        // キャンセル
				        }
			        });
			dlg.show();
		}
			break;
		case R.id.Button02: {
			finish();
		}
			break;
		}
	}    
}
